package com.springboot.springautowired.javaDesignModel.strategy.solution;

import com.springboot.springautowired.javaDesignModel.strategy.issue.IPay;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * 动态拼接名称    该方法主要针对code是有业务含义的场景。
 * 实现这个接口也是获取spring bean的方式之一
 * @author mac
 * @date 2021/9/18 5:02 下午
 */
@Service
public class PayService3 implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    private static final String SUFFIX = "Pay";

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void toPay(String payCode) {
        ((IPay) applicationContext.getBean(getBeanName(payCode))).pay();
    }

    public String getBeanName(String payCode) {
        return payCode + SUFFIX;
    }

    /**
     * 策略+工厂
     * @param code
     */
    public void toPay1(String code) {
        PayStrategyFactory.get(code).pay();
    }
}
